package com.zhihuichongcheng.application.ui.main.fragment;

import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseFragment;
import com.zhihuichongcheng.application.bean.coordination.CoordinationFileBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.coordination.activity.CoordinationDetailActivity;
import com.zhihuichongcheng.application.ui.coordination.activity.CoordinationListActivity;
import com.zhihuichongcheng.application.ui.coordination.activity.CoordinationSearchActivity;
import com.zhihuichongcheng.application.ui.coordination.adapter.CoordinationAdapter;
import com.zhihuichongcheng.application.ui.mine.UserInfoActivity;

import java.util.HashMap;
import java.util.List;

/**
 * 协同fragment Created by 3water on 2016/10/27.
 */

public class CoordinationFragment extends BaseFragment implements View.OnClickListener {
    private ImageView mImUser, mImSearch, mImDownload;
    private XRecyclerView mRcvCoordination;
    private CoordinationAdapter mAdapter;
    private int mPage = 1;
    private int mPageSize = 15;

    @Override
    protected void init() {
        super.init();
        initView();
        initData();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.coordination_headview, mRcvCoordination, false);
        mImDownload = (ImageView) mView.findViewById(R.id.im_download);
        mImDownload.setOnClickListener(this);
        mImUser = (ImageView) mView.findViewById(R.id.im_user);
        mImSearch = (ImageView) view.findViewById(R.id.im_search);
        mImUser.setOnClickListener(this);
        mImSearch.setOnClickListener(this);
        mRcvCoordination = (XRecyclerView) mView.findViewById(R.id.rcv_coordination);
        mRcvCoordination.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CoordinationAdapter(mContext);
        mAdapter.setOnItemOnclick(new OnItemOnclick<CoordinationFileBean>() {
            @Override
            public void onClick(CoordinationFileBean coordinationFileBean, int position) {
                int type = coordinationFileBean.getFileType();
                if (type == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", coordinationFileBean.getFileName());
                    bundle.putString("id", coordinationFileBean.getFileContentId());
                    startActivity(CoordinationDetailActivity.class, bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", coordinationFileBean.getFileName());
                    bundle.putString("id", coordinationFileBean.getFileContentId());
                    startActivity(CoordinationListActivity.class, bundle);
                }
            }
        });
        mRcvCoordination.setAdapter(mAdapter);
        mRcvCoordination.addHeaderView(view);
        mRcvCoordination.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                loadData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });

    }

    private void initData() {
        loadData(mPage);
    }

    private void loadData(final int page) {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", mUser.getToken());
        params.put("directoryId", "0");
        params.put("pageRows", "" + mPageSize);
        params.put("curPage", "" + page);
        manager.post(NetworkAPI.Coordination_list, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<CoordinationFileBean> list = GsonUtil.paraeFromStringToList(value, CoordinationFileBean.class);
                if (page == 1) {
                    mAdapter.clear();
                }
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvCoordination.loadMoreComplete();
                mRcvCoordination.refreshComplete();
                if (list.size() < mPageSize) {
                    mRcvCoordination.setNoMore(true);
                }
            }

            @Override
            public void onFailure(String value) {

            }
        }, params);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_coordination;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_user:
                startActivity(UserInfoActivity.class, null);
                break;
            case R.id.im_search:
                startActivity(CoordinationSearchActivity.class, null);
                break;
            case R.id.im_download:
                startActivityByIntent(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
                break;
            default:
                break;
        }
    }
}
